package com.lienlq.voicemusic.ui;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.lienlq.voicemusic.entity.Album;
import com.lienlq.voicemusic.entity.Genre;
import com.lienlq.voicemusic.entity.Track;
import com.lienlq.voicemusic.entity.TrackAdapter;
import com.lienlq.voicemusic.ui.GenreFragment.ViewHolder;
import com.lienlq.voicemusic.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle; 
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AlbumFragment extends Fragment {
	
	private int width = 0;
	 String NO_AVATAR = "no_avatar";
	  private ArrayList<Album> albums = new ArrayList<Album>(); 
	  private Context context = null;
	    
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);  
	        loadContent();
	    } 
	    
	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    	View gridView = inflater.inflate(R.layout.fragment_album, container, false);
	    	GridView gV = (GridView)gridView.findViewById(R.id.gridalbum_on_album_frag);
	    	if (context == null) context = getActivity();
	        gV.setAdapter(new AlbumAdapter());	  
	        return gridView;
	    }

	    @Override
	    public void onSaveInstanceState(Bundle outState) {
	        super.onSaveInstanceState(outState);
	         
	    }
	    
	    void loadContent() {
	    	for(int i = 0; i<5; i++)  {
	    		albums.add(new Album("One More Night (Remixs) - EP", NO_AVATAR, 12345, "Maroon 5")); 
	    		albums.add(new Album("ABC", NO_AVATAR, 67890, "Big C"));
	    		if (i == 3 || i == 1) {
	    			String imgUrl = "http://image.mp3.zdn.vn/thumb/165_165/covers/4/1/41bd52c4615d290f1f7061f7b67b93de_1365737831.jpg";
	    			String imgUrl2 = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/s32x32/275696_100003639116940_1963795429_q.jpg";
	    			
	    			albums.add(new Album("One More Night (Remixs) - EP", imgUrl, 12345, "Maroon 5")); 
		    		albums.add(new Album("ABC", imgUrl2, 67890, "Big C"));	  
	    		}
	    	}
	    }
	     
		class AlbumAdapter extends ArrayAdapter<Album>
		{
			public AlbumAdapter()
			{
				super(context, R.layout.item_album_on_album_frag, albums);
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) { 
				View v = convertView; 
				if (v == null) {
		            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		            v = vi.inflate(R.layout.item_album_on_album_frag, null);
				}  
				Album item = albums.get(position);
				width = v.getMeasuredWidth();
				
				if(item != null) {
					ImageView avatarImg = (ImageView)v.findViewById(R.id.avatar_album_img_on_item_album_frag);
					TextView artist = (TextView)v.findViewById(R.id.artist_name_tv_on_item_album_frag);
					TextView album = (TextView)v.findViewById(R.id.album_name_tv_on_item_album_frag);
					TextView amount = (TextView)v.findViewById(R.id.amount_tv_item_album_frag);
					WebView avatarWeb = (WebView)v.findViewById(R.id.avater_album_web);
					 
//					if (!item.getAvatarLink().equals(NO_AVATAR)) { 
////						avatarWeb.loadUrl(item.getAvatarLink()); 
////						avatarWeb.loadData(item.createData(), "text/html", "utf-8");
//						 
//					} else {
						avatarImg.setImageResource(R.drawable.unknown2);
						Log.i("IMAGE", "load image");
//					}
					artist.setText(item.getArtist());
					album.setText(item.getName());
					amount.setText(Integer.toString(item.getNumberListen()));
				}  
				return v;
				
			}
			
		}
		 
}
